package Service;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pojo.PageBean;
import pojo.Research;

/**
 * Servlet implementation class ResearchServlet
 */
@WebServlet("/ResearchServlet")
public class ResearchServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ResearchServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		String method=request.getParameter("method");
		System.out.println("1111"+method);
		switch(method) {
		case "showAll":
			showAll(request,response);
			break;
		case "add":
			add(request,response);
			break;
		}
	}
		private void showAll(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException {
			request.setCharacterEncoding("utf-8");
	    	int currentPage = Integer.parseInt(request.getParameter("currentPage"));
	    	int pageSize=5;
	    	ResearchService ps = new ResearchService();
	    	PageBean<Research> pb = ps.show(currentPage, pageSize);
	    	System.out.println(pb);
	    	request.setAttribute("pb", pb);
	    	try {
				try {
					request.getRequestDispatcher("5.jsp").forward(request, response);
				} catch (ServletException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    }
		private void add(HttpServletRequest request, HttpServletResponse response) {
			// TODO Auto-generated method stub
			try {
				request.setCharacterEncoding("utf-8");
			} catch (UnsupportedEncodingException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			Research p =new Research();
			p.setSname(request.getParameter("sname"));
			p.setEmcee(request.getParameter("emcee"));
			p.setUnit(request.getParameter("unit"));
			p.setPsource(request.getParameter("psource"));
//			p.setTime(request.getParameter("time"));
			p.setLevel(request.getParameter("level"));
//			p.setAtime(request.getParameter("atime"));
			System.out.println(p);
			ResearchService pservice = new ResearchService();
			int i = pservice.add(p);
			if(i>0) {
				try {
					response.sendRedirect("ResearchServlet?method=showAll&currentPage=1");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}else {
				try {
					response.sendRedirect("keyan.jsp");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
				}

	}
